package com.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.entity.ModulesEntity;
import com.example.entity.ModulesTreeNode;
import com.example.service.ModulesServlice;

@RequestMapping("/modules")
@Controller
public class ModulesController {

	@Autowired
	private ModulesServlice modules;
	
	@RequestMapping(path="/findAll")
	@ResponseBody
	public Map<String, Object> findAll(String name) {
		Map<String, Object> relMap=new HashMap<String, Object>();
		List<ModulesEntity> list=modules.findAll(name);
		relMap.put("code", "0");
		relMap.put("msg", "success");
		relMap.put("count", "");
		relMap.put("data", list);
		return relMap;
	}
	
	@RequestMapping(path="/findRM")
	@ResponseBody
	public List<ModulesTreeNode> findRM(Integer id) {
		return modules.findRM(id);
	}
	
	/**
	 * 查询该用户所拥有的权限的模板
	 * @param rolesIds
	 * @return
	 */
	public List<ModulesTreeNode> find(String rolesIds) {
		return modules.find(rolesIds);
	}
	/**
	* 生成模块类型树
	* @return List
	*/
	@RequestMapping("/getModule")
	@ResponseBody
	public List<ModulesTreeNode> getModulesTreeNode(Integer id,String rolesIds,String token) {
		List<ModulesTreeNode> modulesTreeList=null;
		if(id!=null && id!=0) {
			modulesTreeList = this.findRM(id);
		}else {
			modulesTreeList = this.find(rolesIds);
		}
//		List<ModulesTreeNode> modulesTreeList = this.find(rolesIds);
		List<ModulesTreeNode> ModulesTreeNodes = new ArrayList<ModulesTreeNode>();
		for (ModulesTreeNode modulesTreeNode : modulesTreeList) {
			if (modulesTreeNode.getParentid()==0) {
				ModulesTreeNodes.add(findChildren(modulesTreeNode, modulesTreeList));
			}
		}
		return ModulesTreeNodes;
	}
	
	/**
	* 查找子类型
	* @return List
	*/
	public ModulesTreeNode findChildren(ModulesTreeNode modulesTreeNode, List<ModulesTreeNode> modulesTreeList) {
		for (ModulesTreeNode item : modulesTreeList) {
			 if (modulesTreeNode.getId()==item.getParentid()) {
				 if(modulesTreeNode.getChildren()==null) {
					 modulesTreeNode.setChildren(new ArrayList<ModulesTreeNode>());
				 } 
				 modulesTreeNode.getChildren().add(findChildren(item,modulesTreeList));
			 } 
		}
		return modulesTreeNode;
	}
	
	/**
	 * 新增/修改
	 * @param me
	 * @return
	 */
	@RequestMapping("/module")
	@ResponseBody
	public Map<String, Object> addModules(ModulesEntity me){
		int count=0;
		if(me.getId()==0) {
			count = modules.addModules(me);
		}else {
			count = modules.upModules(me);
		}
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"提交成功":"提交失败");
		return relMap;
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delModules")
	@ResponseBody
	public Map<String, Object> delModules(Integer id){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int count = modules.delModules(id);
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"删除成功":"删除失败");
		return relMap;
	}
}
